package com.cqupt.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqupt.listener.WordListener;
import com.cqupt.mapper.SysUserMapper;
import com.cqupt.model.constant.Constant;
import com.cqupt.model.dto.AdminLoginDto;
import com.cqupt.model.dto.AdminRegDto;
import com.cqupt.model.dto.AdminUpdatePsdDto;
import com.cqupt.model.pojo.Admin;
import com.cqupt.model.pojo.SysUser;
import com.cqupt.model.pojo.Word;
import com.cqupt.model.vo.AdminVo;
import com.cqupt.model.vo.Result;
import com.cqupt.model.vo.ResultCodeEnum;
import com.cqupt.service.AdminService;
import com.cqupt.mapper.AdminMapper;
import com.cqupt.service.SysUserService;
import com.cqupt.service.WordBookService;
import com.cqupt.service.WordService;
import com.cqupt.utils.AuthContextUtil;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;
import java.util.concurrent.TimeUnit;


@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin>
        implements AdminService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private WordService wordService;

    @Resource
    private WordBookService wordBookService;

/*
    @Resource
    private WordListener wordListener;
*/


    @Override
    public Result login(AdminLoginDto adminLoginDto) {

        String username = adminLoginDto.getUsername();
        LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Admin::getUsername, username);
        Admin admin = getOne(wrapper);
        if (admin == null) {
            return Result.build(null, ResultCodeEnum.LOGIN_ERROR);
        }
        if (!admin.getPassword().equals(adminLoginDto.getPassword())) {
            return Result.build(null, ResultCodeEnum.LOGIN_ERROR);
        }
        String token = UUID.randomUUID().toString().replaceAll("-", "");
        String key = Constant.REDIS_TOKEN_KEY + token;
        if (adminLoginDto.getRememberMe() == 1) {
            stringRedisTemplate.opsForValue().set(key, JSON.toJSONString(admin), Constant.DEFAULT_TOKEN_EXPIRE_TIME, TimeUnit.DAYS);
        } else {
            stringRedisTemplate.opsForValue().set(key, JSON.toJSONString(admin), Constant.DEFAULT_TOKEN_EXPIRE_TIME, TimeUnit.MINUTES);
        }
        AdminVo adminVo = new AdminVo();
        adminVo.setId(admin.getId());
        adminVo.setToken(token);
        return Result.build(adminVo, ResultCodeEnum.SUCCESS);
    }

    @Override
    public Result register(AdminRegDto adminLoginDto) {

        String username = adminLoginDto.getUsername();
        Admin admin = getOne(new LambdaQueryWrapper<>(Admin.class).eq(Admin::getUsername, username));
        if (admin != null) {
            return Result.build(null, ResultCodeEnum.USER_NAME_IS_EXISTS);
        }
        if (!adminLoginDto.getPassword().equals(adminLoginDto.getConfirmPassword())) {
            return Result.build(null, ResultCodeEnum.PWD_IS_NOT_SAME);
        }
        Admin sys_admin = new Admin();
        sys_admin.setUsername(username);
        sys_admin.setPassword(adminLoginDto.getPassword());
        save(sys_admin);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Override
    public Result logout(String token) {
        stringRedisTemplate.delete(Constant.REDIS_TOKEN_KEY + token);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Override
    public Result updatePassword(AdminUpdatePsdDto adminUpdatePsdDto, String token) {
        Integer adminId = adminUpdatePsdDto.getAdminId();
        Admin sysUser = getById(adminId);
        if (!sysUser.getPassword().equals(adminUpdatePsdDto.getOldPsd())) {
            return Result.build(null, ResultCodeEnum.DATA_ERROR);
        }
        if (!adminUpdatePsdDto.getNewPsd().equals(adminUpdatePsdDto.getNewPsd2())) {
            return Result.build(null, ResultCodeEnum.PWD_IS_NOT_SAME);
        }
        sysUser.setPassword(adminUpdatePsdDto.getNewPsd());
        updateById(sysUser);
        stringRedisTemplate.delete(Constant.REDIS_TOKEN_KEY + token);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Override
    public void status(Integer id, Integer status) {
        SysUser sysUser = sysUserMapper.selectById(id);
        sysUser.setIsDeleted(status);
        sysUserMapper.updateById(sysUser);
    }

    @Override
    public void importWord(MultipartFile file, Integer bookId) {
        try {
            EasyExcel.read(file.getInputStream(), Word.class, new WordListener(wordService, wordBookService, bookId)).doReadAll();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}




